package com.nanyang.tms.manager.waybill.handler;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.handler.AbstractTransHandler;
import com.nanyang.common.tool.utils.date.DateUtils;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.waybill.WaybillTrackDao;
import com.nanyang.tms.domain.forecast.context.CalculateFreightContext;
import com.nanyang.tms.domain.waybill.WaybillTrackDO;
import com.nanyang.tms.domain.waybill.context.SaveTackInfoContext;
import com.nanyang.tms.domain.waybill.context.SaveWaybillReceiptContext;
@Component
public class SaveTackInfoHandler  extends AbstractTransHandler{
	@Autowired
	private WaybillTrackDao waybillTrackDao;
	@Autowired
	private GlobalIdGeneratorUtil globalIdGeneratorUtil;
	@Override
	public boolean isAsync() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean handle(TransHandlerContext context) {
		SaveTackInfoContext saveTackInfoContext = (SaveTackInfoContext) context
				.getInnerContext(SaveTackInfoContext.class);
		try {
			waybillTrackDao.insert(bulidWaybillTrack(saveTackInfoContext));
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}
		return true;
	}
	
	private WaybillTrackDO bulidWaybillTrack(SaveTackInfoContext saveTackInfoContext){
		String recordSeq = TmsSerializeConstant.WAYBILL_TRACK_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.WAYBILL_TRACK_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
		WaybillTrackDO waybillTrack = WaybillTrackDO.builder()
		.trackCode(recordSeq)
		.waybillCode(saveTackInfoContext.getWaybillCode())
		.supplierNo(saveTackInfoContext.getSupplierNo())
		.companyCode(saveTackInfoContext.getCompanyCode())
		.content(saveTackInfoContext.getContent())
		.address(saveTackInfoContext.getAddress())
		.trackTime(saveTackInfoContext.getTrackTime())
		.build();
		waybillTrack.setIp(saveTackInfoContext.getIp());
		waybillTrack.setGmtCreatePin(saveTackInfoContext.getPin());
		waybillTrack.setGmtModifiedPin(saveTackInfoContext.getPin());
		return waybillTrack;
	}

}
